﻿<?php
	require_once("../class/mysql_help.class.php");
	require_once("../config/config.php");
	class User
	{
		var $id,$username,$password,$name,$sex,$school,$major,$graduated,$edu_background,$enrol_year,$email,$mobile,$attest;
		var $dbHelper;//定义数据库操作帮助类
		
		//构造函数
		public function __construct(){
			$this->dbHelper = new db_Mysql();
		}
		
		public function Fill_Data($id,$username,$password,$name,$sex,$school,$major,$graduated,$edu_background,$enrol_year,$email,$mobile,$attest)
		{
			$this->id = $id;
			$this->username = $username;
			$this->password = $password;
			$this->name = $name;
			$this->sex = $sex;
			$this->school = $school;
			$this->major = $major;
			$this->graduated = $graduated;
			$this->edu_background = $edu_background;
			$this->enrol_year = $enrol_year;
			$this->email = $email;
			$this->mobile = $mobile;
			$this->attest = $attest;
		}
		
		/*Add*/
		
		
		/*功能：添加用户
		//返回：新增用户的id
		*/
		public function AddUser($username,$password,$name,$email)
		{
			global $pre_md5;//将加密前缀从全局变量里引入进来
			$this->dbHelper->dbconnect();//连接数据库
			$attest = false;//新注册的用户默认为未认证
			$cmd = "Insert Into graduate(user_name,password,name,email,attest)".
				"values('".$username."','".md5($password.$pre_md5)."','".$name."','".$email."','".$attest."')";
			try
			{
				$result = $this->dbHelper->insert($cmd);//执行插入,返回新增记录的id
				$this->dbHelper->dbclose();//关闭数据库
				return $result;//返回新插入记录的id
			}
			catch(Exception $e)
			{
				return null;
			}
		}
		
		/*public function AddUser($id,$name,$sex,$school,$major,$graduated,$edu_background,$enrol_year,$email)
		{
			this->$dbHelper->dbconnect();//连接数据库
			String cmd = "Insert Into graduate(id,name,sex,school,major,graduated,edu_background,enrol_year,email)".
				"values('".$id."','".$name."','".$sex."','".$school."','".$major."','".$graduate."','".
				$edu_background."','".$enrol_year."','".$email."')";
			$result = this->$dbHelper->insert(cmd);//执行插入,返回新增记录的id
		}*/
		
		/*Delete*/
		
		
		/*功能：删除用户
		//返回：受影响的记录数
		*/
		public function DeleteUser(int $userId)
		{
			$this->dbHelper->dbconnect();//连接数据库
			$cmd = "Delete From graduate Where id='".$userId."'";
			$result = $this->dbHelper->delete($cmd);
			$this->dbHelper->dbclose();
			return $result;//返回受影响的记录数
		}
		
		
		/*update*/
		
		/*功能：用户自己修改资料(email不允许修改，因为认证过)
		//返回：受影响的记录数
		*/
		public function UpdateUser($id,$name,$sex,$school,$major,$graduated,$edu_background,$enrol_year,$mobile)
		{
			$this->dbHelper->dbconnect();
			$cmd = "Update graduate set name='".$name."',sex='".$sex."',school='".$school."',".
				"major='".$major."',graduate='".$graduated."',edu_background='".$edu_background."',".
				"enrol_year='".$enrol_year."',email='".$email."' Where id='".$id."'";
			$result = $this->dbHelper->update($cmd);
			$this->dbHelper->dbclose();
			return $result;//返回受影响的记录数
		}
		
		/*功能：用户更改密码
		//返回：受影响的记录数
		*/
		public function UpdatePassword($id,$password)
		{
			$this->dbHelper->dbconnect();
			$cmd = "Update graduate set password='".$password."' Where id='".$id."'";
			$result = $this->dbHelper->update($cmd);
			$this->dbHelper->dbclose();
			return $result;//返回受影响的记录数
		}
		
		/*功能：管理员将该用户设置为认证过
		//返回：受影响的记录数
		*/
		public function UpdateUser_BeAttest($id)
		{
			$this->dbHelper->dbconnect();
			$beAttest = 1;
			$cmd = "Update graduate set attest='".$beAttest."' Where id='".$id."'";
			$result = $this->dbHelper->update($cmd);
			$this->dbHelper->dbclose();
			return $result;//返回受影响的记录数
		}
		
		/*功能：管理员将该用户设置为未认证过
		//返回：受影响的记录数
		*/
		public function UpdateUser_NoAttest($id)
		{
			$this->dbHelper->dbconnect();
			$beAttest = false;
			$cmd = "Update graduate set attest='".$beAttest."' Where id='".$id."'";
			$result = $this->dbHelper->update($cmd);
			$this->dbHelper->dbclose();
			return $result;//返回受影响的记录数
		}
		
		/*Select*/
		
		/*功能：登陆验证
		//返回：user对象/false。代表登陆成功/失败
		*/
		public function Login($userName,$password)
		{
			global $pre_md5;//将加密前缀从全局变量里引入进来
			$this->dbHelper->dbconnect();
			$cmd = "Select * From graduate Where user_name = '".$userName."'";
			$result = $this->dbHelper->execute($cmd);//执行查询返回查询结果集
			$row = $this->dbHelper->fetch_array($result);
			$this->dbHelper->free_result($result);//将查询结果集资源释放
			$us = is_array($row);//看该userName搜索的结果返回是否是数组，是说明返回正确。
			$this->dbHelper->dbclose();
			$ps = $us?(md5($password.$pre_md5)==$row["password"]):false;//将密码加密后于数据库的密码对比，若相同则返回正确
			if($ps)
			{
				$user_id = $row["id"];
				return $user_id;
			}
			else return false;//账号、密码错误
		}
		
		/*功能：查看用户名是否已存在
		//返回：true代表存在该用户，不能注册。/  false  代表该用户名不存在
		*/
		public function IsUserNameExist($userName)
		{
			$this->dbHelper->dbconnect();
			$cmd = "Select * From graduate Where user_name = '".$userName."'";
			$result = $this->dbHelper->get_rows($cmd);
			$this->dbHelper->dbclose();
			if($result > 0)//该用户名已存在
			{
				return true;
			}
			else return false;
		}
		
		/*功能：搜出所有未被验证的毕业生
		//返回：搜索符合要求的记录$result
		//注意：使用完返回的$result记得free哦！
		*/
		public function GetAllGraduator_NoVerifying()
		{
			$cmd = "Select graduate.*,university.name as name_school 
				From graduate Left Join university On graduate.school = university.id Where attest = 0";
			$this->dbHelper->dbconnect();
			$result = $this->dbHelper->execute($cmd);
			$this->dbHelper->dbclose();
			return  $result;
		}
		
		/*功能：将所有未被验证的毕业生罗列出来
		//返回：封装好的html代码
		*/
		public function LayoutGraduate_NoVerifying()
		{
			$result = $this->GetAllGraduator_NoVerifying();
			$str = "";
			
			while($row = $this->dbHelper->fetch_array($result))
			{
				if($row["name"] == null || $row["name"] == "") $row["name"] = "未填";
				if($row["name_school"] == null || $row["name_school"] == "") $row["name_school"] = "未填";
				if($row["enrol_year"] == null || $row["enrol_year"] == "") $row["enrol_year"] = "未填";
				$str.='
					<div class="check_member_list">
						<div class="check_member_box">'.$row["name"].'</div>
						<div class="check_member_box">'.$row["name_school"].'</div>
						<div class="check_member_box">'.$row["enrol_year"].'</div>
						<div class="check_member_box">姚明</div>
						<div class="check_member_box2"><a href="../action/action_graduate_verifying.php?id_graduate='.$row["id"].'">认证</a></div>
					</div>';
			}
			return $str;
		}
		
		/*功能：判断用户是否已经过验证
		@ id_graduate 毕业生的id
		//验证过则返回1，否则返回0
		*/
		public function JudgeGraduateAttest($id_graduate)
		{
			$cmd = "Select attest From graduate Where id = '".$id_graduate."'";
			$this->dbHelper->dbconnect();
			$result = $this->dbHelper->execute($cmd);
			$this->dbHelper->dbclose();
			$row = $this->dbHelper->fetch_array($result);
			$this->dbHelper->free_result($result);
			return $row["attest"];
		}
		
		/*功能：根据用户id返回一条用户的完整信息
		//返回：因为调用此函数时，已经初始化user对象。所以查询出数据后填充当前user对象即可。
		*/
		public function GetUserDataById($userId)
		{
			$this->dbHelper->dbconnect();
			$cmd = "Select * From graduate Where id = '".$userId."'";
			$result = $this->dbHelper->execute($cmd);
			$row = $this->dbHelper->fetch_array($result);
			$this->Fill_Data($row["id"],$row["username"],$row["password"],$row["name"],$row["sex"],$row["school"],
				$row["major"],$row["graduated"],$row["edu_background"],$row["enrol_year"],$row["email"],$row["mobile"],$row["attest"]);
			$this->dbHelper->free_result($result);
		}
		
		/*功能：根据用户id返回该用户的姓名
		@ $userId  用户的id
		//返回：用户的姓名String
		*/
		public function GetUserNameById($userId)
		{
			$cmd="Select name From graduate Where id ='".$userId."'";
			$this->dbHelper->dbconnect();
			$result = $this->dbHelper->execute($cmd);
			$this->dbHelper->dbclose();
			$row = $this->dbHelper->fetch_array($result);
			$this->dbHelper->free_result($result);
			return (string)$row["name"];
		}
	}
?>
		
		
		
		
		
		
		
		